angular.module('app.controllers', ['pascalprecht.translate','cordovaHTTP','highcharts-ng'])

.controller('FxCtrl', function($scope,$translate,assetSrv,cftcGroups) {
    
    $scope.vm = {
        "assetList" : assetSrv.divideAssets(cftcGroups.fx)
    }

})

.controller('CommodityCtrl', function($scope,$translate,assetSrv,cftcGroups) {
    $scope.vm = {
        "assetList" : assetSrv.divideAssets(cftcGroups.commodity)
    }


})

.controller('IndexCtrl', function($scope,$translate,assetSrv,cftcGroups) {
    $scope.vm = {
        "assetList" : assetSrv.divideAssets(cftcGroups.index)
    }
})

.controller('ChartDetailCtrl', function($scope, $stateParams,$ionicLoading,$filter,cftcGroups,cordovaHTTP) {
    var $translate = $filter('translate');

    $scope.vm = {}

    var assets = cftcGroups[$stateParams.type];
    angular.forEach(assets, function(value, key){
        if(value.url == $stateParams.assetId){
            $scope.vm.pageTitle = value.name;
        }
    });

    $scope.chartConfig = {
        options: {
            chart: {
                alignTicks: false
            },
            rangeSelector: {
                enabled: true,
                selected: 2/*,
                buttons: [ {
                    type: 'month',
                    count: 6,
                    text: '6m'
                }, {
                    type: 'ytd',
                    text: 'YTD'
                }, {
                    type: 'year',
                    count: 1,
                    text: '1y'
                }, {
                    type: 'all',
                    text: 'All'
                }]*/
            },
            navigator: {
                enabled: true
            }
        },
        series: [{
            type: 'column',
            name: $scope.vm.pageTitle,
            data: [],
            dataGrouping: {
                units: [[
                    'week', // unit name
                    [1] // allowed multiples
                ], [
                    'month',
                    [1, 2, 3, 4, 6]
                ]]
            }
        }],
        title: {
            text: $scope.vm.pageTitle + $translate('TITLE_SUFFIX')
        },
        useHighStocks: true
    }
    
    $ionicLoading.show({
      template: $translate('LOADING')
    });

    cordovaHTTP.get(cftcGroups.baseUrl + $stateParams.assetId, {}, {}).then(function(response){
        response.data = JSON.parse(response.data);
        //console.log(response);
        /*$scope.chartConfig.series.push({
            type: 'column',
            name: $scope.vm.pageTitle,
            data: response.data.data,
            dataGrouping: {
                units: [[
                    'month',
                    [6]
                ],[
                    'year',
                    [1,2,5]
                ]]
            }
        });*/
        $scope.chartConfig.series[0].data = response.data.data;

        $ionicLoading.hide();
    });

    
    /*$(function () {
        $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-v.json&callback=?', function (data) {

            // create the chart
            $('#container').highcharts('StockChart', {
                chart: {
                    alignTicks: false
                },

                rangeSelector: {
                    selected: 1
                },

                title: {
                    text: 'AAPL Stock Volume'
                },

                series: [{
                    type: 'column',
                    name: 'AAPL Stock Volume',
                    data: data,
                    dataGrouping: {
                        units: [[
                            'week', // unit name
                            [1] // allowed multiples
                        ], [
                            'month',
                            [1, 2, 3, 4, 6]
                        ]]
                    }
                }]
            });
        });
    });*/



    

});
